﻿
Partial Class Controls_RecipientsCopyCut
    Inherits System.Web.UI.UserControl
    Dim u As MembershipUser

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        u = Membership.GetUser(HttpContext.Current.User.Identity.Name)
        FormatGridColumns(GridView2, DropDownList2)
        FormatGridColumns(GridView22, DropDownList22)

    End Sub
    Protected Sub MyEntityODS_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub

    'Protected Sub ObjectDataSource2_Selecting(sender As Object, e As ObjectDataSourceSelectingEventArgs) Handles ObjectDataSource2.Selecting
    '    e.InputParameters("UserId") = u.ProviderUserKey
    'End Sub
    Protected Sub MyEntityODS1_Selecting(sender As Object, e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles MyEntityODS1.Selecting
        e.InputParameters("UserId") = u.ProviderUserKey
    End Sub


    Sub FormatGridColumns(grid As GridView, ddlist As DropDownList)
        Dim columnnum = 0
        Dim i As Integer = 1
        If grid.ID = "GridView2" Then
            columnnum = 19
            i = 1
        Else
            columnnum = 18
            i = 0
        End If

        If IsNumeric(ddlist.SelectedValue) Then
            Dim colnamesTA As New LogginsLogDSTableAdapters.usp_GetTargetListColumnNamesByTargetListIdTableAdapter()
            Dim colnamesDT As LogginsLogDS.usp_GetTargetListColumnNamesByTargetListIdDataTable
            colnamesDT = colnamesTA.GetData(CType(ddlist.SelectedValue, Integer?))

            For index = 0 To columnnum
                grid.Columns(index).Visible = False
            Next
            For index = i To columnnum
                For Each colnameRow As LogginsLogDS.usp_GetTargetListColumnNamesByTargetListIdRow In colnamesDT
                    If grid.Columns(index).HeaderText = colnameRow.ColumnName Then
                        grid.Columns(index).Visible = True
                    End If
                Next

                i = i + 1
            Next
            If grid.ID = "GridView2" Then
                grid.Columns(0).Visible = True
            Else
                grid.Columns(0).Visible = False
            End If
                grid.Visible = True
        Else
            grid.Visible = False
        End If
    End Sub




    Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click

        For Each row As GridViewRow In GridView2.Rows
            ' Access the CheckBox
            Dim cb As CheckBox = row.FindControl("RecSelector")
            If cb IsNot Nothing AndAlso cb.Checked Then
                Dim RecID As Integer = _
                    Convert.ToInt32(GridView2.DataKeys(row.RowIndex).Value)
                Label1.Text &= String.Format( _
                    "This would have deleted ProductID {0}<br />", RecID)
                '............................................
                Try

                Catch ex As Exception
                    Throw ex
                End Try
                Dim targetlistrecexpTA As New LogginsLogDSTableAdapters.usp_TargetListRecipient_ExpTableAdapter()
                Dim targetlistrecexpDT As LogginsLogDS.usp_TargetListRecipient_ExpDataTable
                targetlistrecexpDT = targetlistrecexpTA.GetData(Nothing)
                Dim checkdupleucate = From t In targetlistrecexpDT.AsEnumerable() Where t.RecipientId = Convert.ToDouble(RecID) AndAlso t.TargetListId = Convert.ToDouble(DropDownList22.SelectedValue) Select t
                Dim newrec = CType(RecID, Integer?)
                Dim newlist = CType(DropDownList22.SelectedValue, Integer?)
                If checkdupleucate.Count = 0 Then
                    If CDbl(RadioButtonList1.SelectedValue) = 0 Then
                        Dim recbyidTA As New LogginsLogDSTableAdapters.usp_RecipientSelectByIdTableAdapter()
                        Dim recbyidDT As LogginsLogDS.usp_RecipientSelectByIdDataTable
                        recbyidDT = recbyidTA.GetData(newrec)
                        Dim newnum As Integer
                        For Each recRow As LogginsLogDS.usp_RecipientSelectByIdRow In recbyidDT

                            Dim rec_recinsertTA As New LogginsLogDSTableAdapters.usp_RecipientSelectByTargetListIdTableAdapter()

                            Dim CustomRefId As String
                            Dim FirstName As String
                            Dim LastName As String
                            Dim HomeEmailAddress As String
                            Dim WorkEmailAddress As String
                            Dim MobileNo As String
                            Dim IsAdult As Boolean
                            Dim Gender As String
                            Dim NI_Number As String
                            Dim SS_Number As String
                            Dim BirthMonth As Byte
                            Dim BirthDay As Byte
                            Dim BirthYear As Short
                            Dim PostCode As String
                            Dim HouseNo As String
                            Dim OccupationNote As String
                            Dim IsLocal As Boolean


                            If recRow.IsCustomRefIdNull Then
                                CustomRefId = String.Empty
                            Else
                                CustomRefId = recRow.CustomRefId
                            End If

                            If recRow.IsFirstNameNull Then
                                FirstName = String.Empty
                            Else
                                FirstName = recRow.FirstName
                            End If

                            If recRow.IsLastNameNull Then
                                LastName = String.Empty
                            Else
                                LastName = recRow.LastName
                            End If
                            If recRow.IsHomeEmailAddressNull Then
                                HomeEmailAddress = String.Empty
                            Else
                                HomeEmailAddress = recRow.HomeEmailAddress
                            End If
                            If recRow.IsWorkEmailAddressNull Then
                                WorkEmailAddress = String.Empty
                            Else
                                WorkEmailAddress = recRow.WorkEmailAddress
                            End If
                            If recRow.IsMobileNoNull Then
                                MobileNo = String.Empty
                            Else
                                MobileNo = recRow.MobileNo
                            End If
                            If recRow.IsIsAdultNull Then
                                IsAdult = False
                            Else
                                IsAdult = recRow.IsAdult
                            End If
                            If recRow.IsGenderNull Then
                                Gender = String.Empty
                            Else
                                Gender = recRow.Gender
                            End If
                            If recRow.IsNI_NumberNull Then
                                NI_Number = String.Empty
                            Else
                                NI_Number = recRow.NI_Number
                            End If
                            If recRow.IsSS_NumberNull Then
                                SS_Number = String.Empty
                            Else
                                SS_Number = recRow.SS_Number
                            End If
                            If recRow.IsBirthMonthNull Then
                                BirthMonth = 0
                            Else
                                BirthMonth = CByte(recRow.BirthMonth)
                            End If
                            If recRow.IsBirthDayNull Then
                                BirthDay = 0
                            Else
                                BirthDay = CByte(recRow.BirthDay)
                            End If

                            If recRow.IsBirthYearNull Then
                                BirthYear = 0
                            Else
                                BirthYear = CByte(recRow.BirthYear)
                            End If
                            If recRow.IsPostCodeNull Then
                                PostCode = String.Empty
                            Else
                                PostCode = recRow.PostCode
                            End If
                            If recRow.IsHouseNoNull Then
                                HouseNo = String.Empty
                            Else
                                HouseNo = recRow.HouseNo
                            End If
                            If recRow.IsOccupationNoteNull Then
                                OccupationNote = String.Empty
                            Else
                                OccupationNote = recRow.OccupationNote
                            End If
                            If recRow.IsIsLocalNull Then
                                IsLocal = False
                            Else
                                IsLocal = recRow.IsLocal
                            End If

                            rec_recinsertTA.usp_RecipientInsert(CustomRefId, _
                                                        FirstName, _
                                                        LastName, _
                                                        HomeEmailAddress, _
                                                        WorkEmailAddress, _
                                                        MobileNo, _
                                                        DateTime.Now, True, _
                                                        IsAdult, _
                                                        Gender, _
                                                        NI_Number, _
                                                        SS_Number, _
                                                        BirthMonth, _
                                                        BirthDay, _
                                                        CType(BirthYear, Byte?), _
                                                        PostCode, _
                                                        HouseNo, _
                                                        OccupationNote, True, newnum)
                        Next





                        targetlistrecexpTA.Insert(newlist, newnum, True)
                    Else
                        Dim getdelid = (From t In targetlistrecexpDT.AsEnumerable() Where t.RecipientId = Convert.ToDouble(newrec) _
                            AndAlso t.TargetListId = Convert.ToDouble(DropDownList2.SelectedValue) Select t).FirstOrDefault().Id
                        targetlistrecexpTA.Delete(getdelid)
                        targetlistrecexpTA.Insert(newlist, newrec, True)
                    End If
                Else
                    Label1.Text &= "Recipient already exist in selected list"
                End If


            Else

            End If
        Next




    End Sub


End Class
